package com.feib.soeasy.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.feib.soeasy.dao.RoleDao;
import com.feib.soeasy.model.Group;
import com.feib.soeasy.model.LabelValue;
import com.feib.soeasy.model.MenuItem;
import com.feib.soeasy.model.Role;
import com.feib.soeasy.service.RoleService;

/**
 * @title (#)RoleServiceImpl.java<br>
 * @description 角色邏輯物件<br>
 * @author Anson Tsai<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/11/14 create by Anson Tsai<br>
 */
@Service("roleService")
public class RoleServiceImpl extends BaseServiceImpl implements RoleService {

    @Resource(name = "roleDao")
    private RoleDao roleDao;

    /*
     * (non-Javdoc)
     * 
     * @see com.feib.soeasy.service.RoleService#getAllRole()
     */
    @Override
    public List<Role> getAllRole() {
        return roleDao.findAll();
    }

    @Override
    public List<LabelValue> findLabelValueByGroupNo(String groupNo) {
        List<LabelValue> result = new ArrayList<LabelValue>();
        List<Role> roles = roleDao.findByGroupNo(groupNo);
        for (Role r : roles) {
            LabelValue lv = new LabelValue();
            lv.setLabel(r.getRoleName());
            lv.setValue(r.getRolePk().toString());
            result.add(lv);
        }

        return result;
    }

    @Override
    public Role getRole(Long pk) {
        // TODO Auto-generated method stub
        return roleDao.findByPk(pk);
    }

    @Override
    public Role saveRole(Role role, Set<MenuItem> menuItems) {
        role.setMenuItems(menuItems);                
        return roleDao.save(role);
    }

    @Override
    public Role saveRole(Role role) {
        return roleDao.save(role);
    }

    /* (non-Javadoc)
     * @see com.feib.soeasy.service.RoleService#countRoleByGroupExcludeAdmin(com.feib.soeasy.model.Group)
     */
    @Override
    public int countRoleByGroupExcludeAdmin(Group group) {
        return this.roleDao.countRoleByGroupExcludeAdmin(group);
    }

}
